/**
 * 使用路由组件时，请先导入依赖map.js
 * **/
 function Router() {
    //
    var currentMap = new Map();
    var util = new Util();

    this.config = function () {
        currentMap.put('#/welcome', {
            name:'欢迎页',
            pageTitle:'欢迎页',
            templateUrl:'/init/welcome.html',
            scripts: ['/init/welcome.js']
        });
        currentMap.put('#/capital/item', {
            name:'资金项',
            pageTitle:'资金项',
            templateUrl:'/capital/item/index.html',
            scripts: ['/pager.js', '/capital/item.js']
        });
    }

    this.reload = function () {
        //
        var currentHash = window.location.hash;
        //alert(currentHash);
        if(currentHash == '') {
            currentHash = '#/welcome';
        }
        var currentRouter = currentMap.get(currentHash);
        document.title = currentRouter.pageTitle + ' 生活管理系统 - 凤松轩';
        //document.getElementById('page-body').innerHTML = currentRouter.name;

        // Ajax载入页面
        $.ajax({
            url: '/html' + currentRouter.templateUrl + '?version=' + Math.random(),
            cache: false,
            success:function (data) {
                //console.log(data);
                document.getElementById('page-body').innerHTML = data;
            },
            error:function (xhr, textStatus) {
                console.log(xhr);
            }
        });

        //动态载入js
        if(currentRouter.scripts != '') {
            // 遍历数组动态加载js
            for(var i = 0; i < currentRouter.scripts.length; i ++) {
                var script = document.createElement('script');
                script.type = 'text/javascript';
                script.src = '/assets/js' + currentRouter.scripts[i] + '?version=' + Math.random();
                document.getElementsByTagName('head')[0].appendChild(script);
            }
        }
    }
}